package simorg.actions;

import simorg.controller.AssignmentController;
import simorg.domain.persistence.AssignmentTable;
import simorg.view.Flash;
import simorg.view.ModelView;
import simorg.view.WebSessionContext;

public class AssignmentDelete extends Action {

	public static String actionName = "delete";
	public AssignmentDelete() {
		super(actionName);
		this.controllerName = AssignmentController.controllerName;
	}
	@Override
	public ModelView checkAccess(WebSessionContext wc) throws Exception {
		return this.checkAdminAccess(wc);
	}
	
	@Override
	public ModelView process(WebSessionContext wc) throws Exception {
		int id = Integer.parseInt(this.getRequestArgs().get(0));
		AssignmentTable.deleteById(wc.getDbConnection(), id);
		Flash flash = new Flash("assignment_result");
		flash.getValues().put("message", "Assignment deleted.");
		wc.addFlash(flash);
		
		Action redirect = new CourseView(String.valueOf(Integer.parseInt(wc.getRequestParameter("courseId"))));
		wc.redirectAdmin(redirect, "enrollment");
		return null;
	}

}
